<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input
              placeholder="请输入内容"
              clearable
              v-model="goodsinfo.query"
              @clear="getgoodslists"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getgoodslists"
              ></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addgoods">添加用户</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="goodslist"
          stripe
          style="width: 100%; margin-top: 15px"
          border
        >
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="1098px">
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)">
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- 编辑按钮 -->
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
              ></el-button>
              <!-- 删除按钮 -->
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removegoods(scope.row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block" style="margin-top: 15px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodsinfo.pagenum"
          :page-sizes="[5, 10, 15, 40]"
          :page-size="goodsinfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totle"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      goodsinfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      goodslist: [],
      //数据总数
      totle: 0,
    }
  },
  mounted() {
    this.getgoodslists()
  },
  methods: {
    //获取商品列表信息
    async getgoodslists() {
      const { data: res } = await this.$http.get('goods', {
        params: this.goodsinfo,
      })
      if (res.meta.status !== 200) return res.$message.error('获取商品信息失败')
      this.goodslist = res.data.goods
      this.totle = res.data.total
      // console.log(res.data)
    },
    // 获取最新的页面个数大小
    handleSizeChange(newSize) {
      this.goodsinfo.pagesize = newSize
      this.getgoodslists()
    },
    handleCurrentChange(newPage) {
      this.goodsinfo.pagenum = newPage
      this.getgoodslists()
    },
    // 删除商品列表
    async removegoods(id) {
      // console.log(id)
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      // 如果用户确认删除则返回字符串confirm,取消则返回cancel
      // console.log(confirmResult)
      if (confirmResult === 'confirm') {
        const { data: res } = await this.$http.delete('goods/' + id)
        if (res.meta.status !== 200) return this.$message.error('用户删除失败')
        // 删除成功重新渲染
        this.$message.success('删除用户成功！')
        this.getgoodslists()
      } else {
        this.$message.info('已取消删除')
      }
    },
    // 添加商品
    addgoods() {
      this.$router.push('/goods/add')
    },
  },
}
</script>
<style lang="less" scoped>
</style>